<script setup>
import { ref } from 'vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { myArticleList } from '@/api/article'
import { formatTime } from '@/util/format'
import ArticleEdit from './components/ArticleEdit.vue'
import { Delete, Edit } from '@element-plus/icons-vue'

const articleList = ref([])
const total = ref(0)
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
const loading = ref(false)
const editRef = ref(null)

const getArticleList = async () => {
  loading.value = true
  const res = await myArticleList(params.value)
  articleList.value = res.data
  total.value = res.total
  loading.value = false
}
getArticleList()

const onAddArticle = () => {
  editRef.value.open({})
}

const onDelArticle = (row, $index) => {
  console.log(row, $index)
}
const onEditArticle = (row) => {
  editRef.value.open(row)
}

const onSizeChange = (pageSize) => {
  params.value.pagesize = pageSize
  onCurrentChange(1)
}

const onCurrentChange = (pageNum) => {
  params.value.pagenum = pageNum
  getArticleList()
}

const onSearch = () => {
  onCurrentChange(1)
}
const onReset = () => {
  params.value.cate_id = ''
  params.value.state = ''
  onCurrentChange(1)
}
const onSuccess = (type) => {
  if (type === 'add') {
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    onCurrentChange(lastPage)
  }
  getArticleList()
}
</script>
<template>
  <PageContainer title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">添加文章</el-button>
    </template>
    <el-form inline="true" class="demo-form-inline">
      <el-form-item label="文章分类：">
        <ChannelSelect v-model:mv="params.cate_id"></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select v-model="params.state">
          <el-option label="已发布" value="已发布" type="primary"></el-option>
          <el-option label="草稿" value="草稿" type="info"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row: { title } }">
          <el-link type="primary" :underline="false">{{ title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发布时间" prop="pub_date">
        <template #default="{ row: { pub_date } }">
          {{ formatTime(pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            @click="onEditArticle(row, $index)"
          /><el-button
            type="danger"
            :icon="Delete"
            circle
            @click="onDelArticle(row)"
          />
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :size="size"
      :disabled="disabled"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: end"
    />

    <!-- 抽屉 -->
    <ArticleEdit ref="editRef" @success="onSuccess"></ArticleEdit>
  </PageContainer>
</template>

<style lang="scss" scoped>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
